{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tushare as ts\n",
    "\n",
    "df=ts.get_area_classified()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "浙江     417\n",
       "江苏     384\n",
       "北京     308\n",
       "广东     302\n",
       "上海     278\n",
       "深圳     270\n",
       "山东     195\n",
       "福建     132\n",
       "四川     118\n",
       "安徽     103\n",
       "湖南     103\n",
       "湖北      96\n",
       "河南      78\n",
       "辽宁      74\n",
       "河北      56\n",
       "新疆      52\n",
       "天津      49\n",
       "重庆      49\n",
       "陕西      47\n",
       "吉林      42\n",
       "山西      38\n",
       "江西      38\n",
       "黑龙江     36\n",
       "广西      36\n",
       "云南      34\n",
       "甘肃      33\n",
       "海南      30\n",
       "贵州      27\n",
       "内蒙      25\n",
       "西藏      16\n",
       "宁夏      13\n",
       "青海      12\n",
       "Name: area, dtype: int64"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['area'].value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "df1=df[(df['area']=='辽宁') | (df['area']=='吉林') | (df['area']=='黑龙江') ]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "def save_to_excel(df,filename,encoding='gbk'):\n",
    "    try:\n",
    "            df.to_csv('temp.csv',encoding=encoding,index=False)\n",
    "            df=pd.read_csv('temp.csv',encoding=encoding,dtype={'code':str})\n",
    "            df.to_excel(filename,encoding=encoding)\n",
    "            return True\n",
    "    except Exception as e:\n",
    "            print(\"Save to excel faile\"\n",)
    "            print(e)\n",
    "            return None"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "save_to_excel(df1,'dongbei.xls')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
